package partlow.yelp.parsers;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import partlow.yelp.datatypes.Business;
import partlow.yelp.datatypes.Category;

/**
 *
 * @author Devin
 */
public class BusinessParser extends YelpTypeParser<Business>{

    @Override
    public Business parse(JSONObject json) throws JSONException {
        Business obj = new Business();
        if(json.has("is_closed"))
            obj.setClosed(json.getBoolean("is_closed"));
        if(json.has("name"))
        	obj.setName(json.getString("name"));
        if(json.has("mobile_url"))
        	obj.setMobileURL(json.getString("mobile_url"));
        if(json.has("snippet_text"))
        	obj.setSnippetText(json.getString("snippet_text"));
        if(json.has("categories")) {
        	JSONArray categoryArray = json.getJSONArray("categories");
        	int categoryArrayLength = categoryArray.length();
        	for(int itemIndex = 0; itemIndex < categoryArrayLength; itemIndex++) {
                // get pair
        		JSONArray categoryPair = categoryArray.getJSONArray(itemIndex);
        		obj.getCategories().add(new Category((String)categoryPair.get(0), (String)categoryPair.get(1)));
        	}
        }
        if(json.has("location")){
            JSONObject location = json.getJSONObject("location");
            obj.setLocation(new LocationParser().parse(location));
        }
        if(json.has("deals"))
            obj.setDeals(new DealParser().parse(json.getJSONArray("deals")));
        
        return obj;
    }

}
